let app = getApp()
// pages/order_list/order_list.js
Page({
  onShareAppMessage: function (res) {
    return {
      desc: '铧佗爱助',
      success: function (res) {

      },
      fail: function (res) {

      }
    }
  },
  /**
   * 页面的初始数据 
   */
  data: {
    goods: [],
    swiper_height: "0",
    collect: 0, //收藏
    salenum: 'active', //销量
    shop_id: '',
    store: [],
    is_show_time: 1,
    store_collect: 0,
    up: false,
    voucher_list: '',
	showadd: false,
	statusBarHeight: app.globalData.statusBarHeight,
    currentNav: 'all' // 默认选中全部商品
  },

  // 导航切换
  switchNav: function(e) {
    const nav = e.currentTarget.dataset.nav;
    this.setData({
      currentNav: nav
    });
    
    // 根据导航类型执行相应操作
    switch(nav) {
      case 'all':
        // 全部商品
        this.all();
        break;
      case 'new':
        // 商品上新
        this.update();
        break;
      case 'sales':
        // 销量排行
        this.salenum();
        break;
      case 'collect':
        // 收藏排行
        this.collect();
        break;
    }
  },

  // 加载默认商品数据（店铺首页）
  loadDefaultGoods: function() {
    var that = this;
    wx.request({
      url: app.configData.shop.api_url + '/index.php?ctl=Shop&met=goodsList&sort=desc&typ=json',
      method: 'GET',
      data: {
        id: that.data.shop_id,
        order: 'common_salenum',
        sort: 'desc',
        type_wxapp: 'wxapp'
      },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        var good_info = res.data.data.items;
        that.setData({
          goods: good_info,
          is_show_time: 0
        });
      }
    });
  },

  // 自定义事件 用来接收子组件传递的数据的
   handleItemChange(e){
       wx.switchTab({
       	url: '../web_view/web_view'
       })
   },

  /**
   * 生命周期函数--监听页面加载
   */

  onLoad: function (options) {
    // toast组件实例
    new app.ToastPannel();

    var k = wx.getStorageSync('key');
    var u = wx.getStorageSync('u');

    var that = this
	
	  var showadd = options.showadd || false
	  this.setData({
		  windowHeight: wx.getStorageSync('windowHeight'),
		  swiper_height: wx.getSystemInfoSync().windowWidth,
	    showadd: showadd
	  });
    
    // 默认加载全部商品数据
    wx.request({
      url: app.configData.shop.api_url + '/index.php?ctl=Shop&met=goodsList&sort=desc&typ=json',
      method: 'GET',
      data: {
        id: options.shop_id,
        order: 'common_salenum',
        sort: 'desc',
        type_wxapp: 'wxapp'
      },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        var good_info = res.data.data.items
        that.setData({
          goods: good_info,
          shop_id: options.shop_id,
          is_show_time: 0
        })
      }
    })
    
    //获取店铺详情页数据
    wx.request({
      url: app.configData.shop.api_url + '/index.php?ctl=Shop&met=getStoreInfo&typ=json',
      method: 'GET',
      data: {
        k: k,
        u: u,
        shop_id: options.shop_id,
      },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        var store_info = res.data.data.store_info
        that.setData({
          store: store_info,
          is_favorate: store_info.is_favorate,
          store_collect: store_info.store_collect
        })
      }
    })
  },

  imgLoad: function (event) {
    var imgwidth = event.detail.width,
      imgheight = event.detail.height,
      //宽高比  
      ratio = imgwidth / imgheight;
    var imgheight = 750 / ratio;
    this.setData({
      swiper_image: imgheight
    })
  },

  //获取收藏数据
  collect: function () {
    var that = this

    wx.request({
      url: app.configData.shop.api_url + '/index.php?ctl=Shop&met=goodsList&sort=desc&typ=json',
      method: 'GET',
      data: {
        id: that.data.shop_id,
        order: 'common_collect',
        sort: 'desc',
        type_wxapp: 'wxapp'
      },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        var good_info = res.data.data.items
        that.setData({
          goods: good_info,
          collect: "active",
          salenum: ""
        })
      }
    })
  },
  //获取销量数据
  salenum: function () {
    var that = this
    wx.request({
      url: app.configData.shop.api_url + '/index.php?ctl=Shop&met=goodsList&sort=desc&typ=json',
      method: 'GET',
      data: {
        id: that.data.shop_id,
        order: 'common_salenum',
        sort: 'desc',
        type_wxapp: 'wxapp'

      },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        var good_info = res.data.data.items
        that.setData({
          goods: good_info,
          collect: "",
          salenum: "active",
          type_wxapp: 'wxapp'
        })
      }
    })
  },
  //取消收藏店铺
  is_favo: function () {
    var that = this
    var k = wx.getStorageSync('key');
    var u = wx.getStorageSync('u');
    wx.request({
      url: app.configData.shop.api_url + '/index.php?ctl=Buyer_Favorites&met=delFavoritesShop&typ=json',
      method: 'GET',
      data: {
        k: k,
        u: u,
        id: that.data.shop_id
      },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {

        that.show('已取消收藏');
        that.setData({
          is_favorate: false,
          store_collect: Number(that.data.store_collect) - 1
        })
      }
    })
  },
  //收藏店铺
  if_favo: function () {
    var that = this
    var k = wx.getStorageSync('key');
    var u = wx.getStorageSync('u');
    if (!k) {
      that.show("您还未绑定商城系统，请重新绑定")
    } else {
      wx.request({
        url: app.configData.shop.api_url + '/index.php?ctl=Shop&met=addCollectShop&typ=json',
        method: 'GET',
        data: {
          k: k,
          u: u,
          shop_id: that.data.shop_id
        },
        header: {
          'content-type': 'application/json'
        },
        success: function (res) {

          that.show('已收藏');
          that.setData({
            is_favorate: true,
            store_collect: Number(that.data.store_collect) + 1
          })
        }
      })
    }
  },
  toProduct: function (e) {
    var goods_id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '../product_detail/product_detail?goods_id=' + goods_id
    })
  },
  //跳转至商品上新
  update: function (e) {
    wx.redirectTo({
      url: '../store_goods_update/store_goods_update?shop_id=' + this.data.shop_id
    })
  },
  //跳转全部商品
  all: function (e) {
    wx.redirectTo({
      url: '../store_goods/store_goods?shop_id=' + this.data.shop_id
    })
  },
  search: function () {
    wx.redirectTo({
      url: '../search_store/search_store?shop_id=' + this.data.shop_id
    })
  },
  // 免费领券
  free_voucher: function () {
    var that = this
    that.setData({
      up: !(that.data.up)
    })
    if (that.data.up == true) {
      wx.request({
        url: app.configData.shop.api_url + '/index.php?ctl=Voucher&met=vList&typ=json',
        method: 'GET',
        data: {
          store_id: that.data.shop_id,
          gettype: 'free'
        },
        header: {
          'content-type': 'application/json'
        },
        success: function (res) {
          if (res.data.status == 200) {
            that.setData({
              voucher_list: res.data.data.voucher.items,
            })
          }
        }
      })
    }
  },
  //领取代金券
  getvoucher: function (e) {
    var that = this
    var k = wx.getStorageSync('key');
    var u = wx.getStorageSync('u');
    var vid = e.target.dataset.vid
    if (vid) {
      wx.request({
        url: app.configData.shop.api_url + '/index.php?ctl=Voucher&met=receiveVoucher&typ=json',
        method: 'GET',
        data: {
          k: k,
          u: u,
          vid: vid
        },
        header: {
          'content-type': 'application/json'
        },
        success: function (res) {
          that.show(res.data.msg)
        }
      })
    } else {
      that.show('领取失败')
    }

  },
  close_mask: function () {
    this.setData({
      up: false
    })
  }

})